package com.cyandpos.pad.sale.saleutils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.changyi.device.DatePattern;
import com.cyandpos.pad.R;
import com.cyandpos.pad.activity.MainActivity;
import com.cyandpos.pad.common.commondata.DialogConstant;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.common.commondata.OnDialogListener;
import com.cyandpos.pad.posdata.DjdData;
import com.cyandpos.pad.posdata.Goods;
import com.cyandpos.pad.posdata.PromationoodInfo;
import com.cyandpos.pad.posdata.SubTicketItem;
import com.cyandpos.pad.posdata.TicketInfo;
import com.cyandpos.pad.sale.adapter.ExchangeGoodBillInfoListAdapter;
import com.cyandpos.pad.sale.presenter.SalePresenterImpl;
import com.cyandpos.pad.sale.view.SelfNumKeyView;
import com.cyandpos.pad.utils.CommonUtils;
import com.cyandpos.pad.utils.MoneyUtils;
import com.cyandpos.pad.utils.ToastUtils;
import com.cyandpos.pad.utils.log.LogUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by xin on 2021/5/18
 * desc:
 */
public class SaleUtil {
    public  static AlertDialog mDialog;
    private static List<PromationoodInfo> mExchangeGoodBillInfoList = new ArrayList<>();
    private static List<PromationoodInfo> mGiftGoodsBillList = new ArrayList<>();
    private static int promotionGoodsSelectSign = 0;

    /**
     * 整单折扣弹框
     * @param mainActivity
     * @param mTicketInfo
     * @param orderTotalMoney
     */
    public static void showFrontDiscountDialog(final MainActivity mainActivity, final TicketInfo mTicketInfo,final int orderTotalMoney) {
        View view = LayoutInflater.from(mainActivity).inflate(R.layout.dialog_goods_frontdiscount, null);
        TextView tvTitle = view.findViewById(R.id.tv_dialog_title_name);
        final SelfNumKeyView sf_containt = view.findViewById(R.id.member_soft_keyboard);

        tvTitle.setText("整单折扣");
        tvTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); //textview的drawableRight

        RadioButton rb_zkl = view.findViewById(R.id.rb_frontDiscount_zkl);//折扣率
        final RadioButton rb_zke = view.findViewById(R.id.rb_frontDiscount_zke);//折扣额
        TextView tv_payMoney = view.findViewById(R.id.tv_frontDiscount_payMoney);//应付金额
        final TextView tv_actuallyMoney = view.findViewById(R.id.tv_frontDiscount_actuallyMoney);//实付金额
        final TextView tv_discountType = view.findViewById(R.id.tv_zkType_name);//折扣名称
        final ImageView iv_discountRate = view.findViewById(R.id.iv_discountRate);//折扣率图片
        final EditText etDiscountRate = view.findViewById(R.id.et_frontDiscount_discountRate);//et折扣率
        final TextView tvDiscountRate = view.findViewById(R.id.tv_frontDiscount_discountRate);//et折扣率
        Button btnSure = view.findViewById(R.id.btn_frontDiscount_ok);//确定
        Button btnCancel = view.findViewById(R.id.btn_frontDiscount_cancel);//退出

        tv_payMoney.setText("￥"+ MoneyUtils.getMoneyStr(orderTotalMoney)+"");
        tv_actuallyMoney.setText("￥"+MoneyUtils.getMoneyStr(orderTotalMoney)+"");
        rb_zke.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_discountType.setText("折扣金额");
                iv_discountRate.setVisibility(View.GONE);

            }
        });

        rb_zkl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_discountType.setText("折  扣  率");
                iv_discountRate.setVisibility(View.VISIBLE);
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });


        etDiscountRate.setFocusable(false);
        sf_containt.setEnterReceptor(etDiscountRate);
        sf_containt.setIsCanInputPoint(true);
        sf_containt.setOnCallBack(new SelfNumKeyView.SelfNumCallBack() {
            @Override
            public void keyBoardSure(String num,String hint) {
                int zkje = 0;
                if(rb_zke.isChecked()){//折扣额
                    if(calculateFront(mTicketInfo,1,num,orderTotalMoney)!=null){
                        zkje = Integer.parseInt(calculateFront(mTicketInfo,1,num,orderTotalMoney));
                        tv_actuallyMoney.setText("￥"+MoneyUtils.getMoneyStr(orderTotalMoney -
                                zkje)+"");
                    }
                }else{//折扣率
                    if(calculateFront(mTicketInfo,0,num,orderTotalMoney)!=null){
                        zkje = Integer.parseInt(calculateFront(mTicketInfo,0,num,orderTotalMoney));
                        tv_actuallyMoney.setText("￥"+MoneyUtils.getMoneyStr(orderTotalMoney -
                                zkje)+"");
                    }
                }

            }
        });

        etDiscountRate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtils.hideSoftInputButWithSelection(etDiscountRate,mainActivity);
                etDiscountRate.setFocusable(true);
                etDiscountRate.setFocusableInTouchMode(true);
                etDiscountRate.requestFocus();

            }
        });

        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                int disType =0;
                String discountInputContext = etDiscountRate.getText().toString();
                if(rb_zke.isChecked()){
                    disType =1;
                }else{
                    double tempDiscountRate = Double.valueOf(etDiscountRate.getText().toString())/10;
                    LogUtils.d("折扣率为："+tempDiscountRate);
                    discountInputContext = tempDiscountRate +"";
                }
                if(calculateFront(mTicketInfo,disType,etDiscountRate.getText().toString(),orderTotalMoney)!=null)
                    mainActivity.mSalePresenterImpl.frontDiscount(disType+"",discountInputContext,null,mTicketInfo.getOrderNo(),false);
            }
        });

        AlertDialog.Builder dialog = new AlertDialog.Builder(mainActivity);
        dialog.setView(view).setCancelable(false).create();
        mDialog = dialog.show();
    }

    private static String calculateFront(TicketInfo mTicketInfo,int disType, String discount,int totalMoney){
        String zkje ="0";
        if (TextUtils.isEmpty(discount) || discount == null){
            return null;
        }
        if(disType == 0){//折扣率
            double tempDiscountRate = Double.valueOf(discount)/100;
            if (tempDiscountRate <= 0 || tempDiscountRate >= 1) {
                ToastUtils.showMsg("请输入正确折扣率", 0);
                return null;
            }
            zkje =  MoneyUtils.Double2Int(totalMoney/100 * (1 - tempDiscountRate))+"";
        }else {
            int tempDiscount = MoneyUtils.getMoneyInt(discount);
            int goodSumMoney = totalMoney;
            if(tempDiscount>goodSumMoney){
                ToastUtils.showMsg("折扣金额不能大于销售金额",0);
                return null;
            }
            zkje = MoneyUtils.getMoneyInt(discount)+"";
        }
        return zkje;
    }

    /**
     * 预算后订单信息弹框
     * @param mainActivity
     * @param ticketInfo
     */
    public static void  showOrderPreferentialInto(final MainActivity mainActivity,final TicketInfo ticketInfo) {
        View view = LayoutInflater.from(mainActivity).inflate(R.layout.dialog_order_budget_info, null);
        TextView tvTitle = view.findViewById(R.id.tv_dialog_title_name);
        tvTitle.setText("整单促销计算");
        tvTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); //textview的drawableRight

        TextView tv_totalMoney = view.findViewById(R.id.tv_budget_order_totalMoney);//总价
        TextView tv_discountMoney= view.findViewById(R.id.tv_budget_order_discountMoney);//折扣
        TextView tv_actualPayMoney= view.findViewById(R.id.tv_budget_order_actualPayMoney);//实付
        TextView tv_taxMone = view.findViewById(R.id.tv_budget_order_taxMoney);//税额

        Button btnSure = view.findViewById(R.id.btn_budget_ok);//确定
        Button btnCancel = view.findViewById(R.id.btn_budget_back);//返回


        LogUtils.d("总价："+ticketInfo.getTotalAmount() );
        tv_totalMoney.setText( MoneyUtils.getMoneyStr(MoneyUtils.getMoneyInt(ticketInfo.getTotalAmount()) + MoneyUtils.getMoneyInt(ticketInfo.getTotalDiscount()) ));
        tv_discountMoney.setText(MoneyUtils.getMoneyStr(MoneyUtils.getMoneyInt(ticketInfo.getTotalDiscount())));
        tv_actualPayMoney.setText( MoneyUtils.getMoneyStr(MoneyUtils.getMoneyInt(ticketInfo.getTotalAmount())));
        tv_taxMone.setText(MoneyUtils.getMoneyStr(0)+"");


        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });

        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                mExchangeGoodBillInfoList.clear();
                mGiftGoodsBillList.clear();
                promotionGoodsSelectSign = 0;
                mainActivity.upLoadGoodsPreparePay(false,mExchangeGoodBillInfoList,mGiftGoodsBillList,promotionGoodsSelectSign);
            }
        });

        AlertDialog.Builder dialog = new AlertDialog.Builder(mainActivity);
        dialog.setView(view).setCancelable(false).create();
        mDialog = dialog.show();

    }

    /**
     * 订金单生成输入信息弹框
     * @param mainActivity
     * @param ticketInfo
     */
    public static void  showDepositInputInfoDialog(final MainActivity mainActivity,final TicketInfo ticketInfo) {
        View view = LayoutInflater.from(mainActivity).inflate(R.layout.dialog_order_deposit_info, null);
        TextView tvTitle = view.findViewById(R.id.tv_dialog_title_name);
        tvTitle.setText("订金单信息");
        tvTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); //textview的drawableRight

        final EditText et_Money = view.findViewById(R.id.et_sale_djd_money);//金额
        final EditText et_phone = view.findViewById(R.id.et_sale_djd_phone);//手机号
        final EditText et_date = view.findViewById(R.id.et_sale_djd_date);//结转日期
        final SelfNumKeyView sf_containt = view.findViewById(R.id.deposit_soft_keyboard);//结转日期

        Button btnSure = view.findViewById(R.id.btn_deposit_ok);//确定
        Button btnCancel = view.findViewById(R.id.btn_deposit_back);//返回

        et_Money.setFocusable(false);
        et_phone.setFocusable(false);
        et_date.setFocusable(false);

        sf_containt.setEnterReceptor(et_Money);
        sf_containt.setIsCanInputPoint(true);
        et_Money.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus){
                    setSelfNumKeyViewEdit(sf_containt,et_Money,mainActivity);
                }else {
                    et_Money.setFocusable(false);
                }
            }
        });
        et_phone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus){
                    setSelfNumKeyViewEdit(sf_containt,et_phone,mainActivity);
                }else {
                    et_phone.setFocusable(false);
                }
            }
        });

        et_date.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus){
                    setSelfNumKeyViewEdit(sf_containt,et_date,mainActivity);
                }
            }
        });

        et_Money.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setSelfNumKeyViewEdit(sf_containt,et_Money,mainActivity);
            }
        });

        et_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setSelfNumKeyViewEdit(sf_containt,et_phone,mainActivity);
            }
        });

        et_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setSelfNumKeyViewEdit(sf_containt,et_date,mainActivity);
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });

        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int totalPrice = 0;
                int totalDiscount = 0;
                for (SubTicketItem sub : ticketInfo.getSubTickets()) {
                    totalPrice += sub.getTotalMoney();
                    totalDiscount += sub.getTotalDiscount();
                }
                int payableAmt = totalPrice - totalDiscount;
                if(CommonUtils.isNullOrEmpty(CommonUtils.getContent(et_Money))||Double.valueOf(CommonUtils.getContent(et_Money))<0
                        ||MoneyUtils.getMoneyInt(CommonUtils.getContent(et_Money))>payableAmt){
                    ToastUtils.showMsg("订金单金额错误！");
                    return;
                }
                if(CommonUtils.isNullOrEmpty(CommonUtils.getContent(et_phone)) || CommonUtils.isMobileNO(CommonUtils.getContent(et_phone))){
                    ToastUtils.showMsg("手机号不正确！");
                    return;
                }
                if(CommonUtils.isNullOrEmpty(CommonUtils.getContent(et_date))){
                    ToastUtils.showMsg("转结日期不能为空！");
                    return;
                }
                String time = DatePattern.mDateFormat2.format(new Date());
                if(CommonUtils.getContent(et_date).length() != 8 ||
                        MoneyUtils.getInt(CommonUtils.getContent(et_date)) < MoneyUtils.getInt(time)||
                        MoneyUtils.getInt(CommonUtils.getContent(et_date).substring(6,8)) > 31){
                    ToastUtils.showMsg("转结日期错误！");
                    return;
                }
                mDialog.dismiss();
                DjdData djdData = new DjdData();
                djdData.setDjd(true);
                djdData.setDjdAmount(CommonUtils.getContent(et_Money));
                djdData.setPhone(CommonUtils.getContent(et_phone));
                djdData.setTransferDate(CommonUtils.getContent(et_date));
                ticketInfo.setDepositOrderInfo(djdData);
                mExchangeGoodBillInfoList.clear();
                mGiftGoodsBillList.clear();
                promotionGoodsSelectSign = 0;
                mainActivity.upLoadGoodsPreparePay(false,mExchangeGoodBillInfoList,mGiftGoodsBillList,promotionGoodsSelectSign);
            }
        });
        sf_containt.setOnCallBack(new SelfNumKeyView.SelfNumCallBack() {
            @Override
            public void keyBoardSure(String num, String hint) {
                if (et_Money.isFocusable()){
                    et_Money.setFocusable(false);
                    et_Money.clearFocus();
                    setSelfNumKeyViewEdit(sf_containt,et_phone,mainActivity);
                }else if (et_phone.isFocusable()){
                    et_phone.setFocusable(false);
                    et_phone.clearFocus();
                    setSelfNumKeyViewEdit(sf_containt,et_date,mainActivity);
                }else if (et_date.isFocusable()){
                    int totalPrice = 0;
                    int totalDiscount = 0;
                    for (SubTicketItem sub : ticketInfo.getSubTickets()) {
                        totalPrice += sub.getTotalMoney();
                        totalDiscount += sub.getTotalDiscount();
                    }
                    int payableAmt = totalPrice - totalDiscount;
                    if(CommonUtils.isNullOrEmpty(CommonUtils.getContent(et_Money))||Double.valueOf(CommonUtils.getContent(et_Money))<0
                            ||MoneyUtils.getMoneyInt(CommonUtils.getContent(et_Money))> payableAmt){
                        ToastUtils.showMsg("订金单金额错误！");
                        return;
                    }
                    if(CommonUtils.isNullOrEmpty(CommonUtils.getContent(et_phone)) || CommonUtils.isMobileNO(CommonUtils.getContent(et_phone))){
                        ToastUtils.showMsg("手机号不正确！");
                        return;
                    }
                    if(CommonUtils.isNullOrEmpty(CommonUtils.getContent(et_date))){
                        ToastUtils.showMsg("转结日期不能为空！");
                        return;
                    }
                    String time = DatePattern.mDateFormat2.format(new Date());
                    if(CommonUtils.getContent(et_date).length() != 8 ||
                            MoneyUtils.getInt(CommonUtils.getContent(et_date)) < MoneyUtils.getInt(time)||
                            MoneyUtils.getInt(CommonUtils.getContent(et_date).substring(6,8)) > 31){
                        ToastUtils.showMsg("转结日期错误！");
                        return;
                    }
                    mDialog.dismiss();
                    DjdData djdData = new DjdData();
                    djdData.setDjd(true);
                    djdData.setDjdAmount(CommonUtils.getContent(et_Money));
                    djdData.setPhone(CommonUtils.getContent(et_phone));
                    djdData.setTransferDate(CommonUtils.getContent(et_date));
                    ticketInfo.setDepositOrderInfo(djdData);
                    mExchangeGoodBillInfoList.clear();
                    mGiftGoodsBillList.clear();
                    promotionGoodsSelectSign = 0;
                    mainActivity.upLoadGoodsPreparePay(false,mExchangeGoodBillInfoList,mGiftGoodsBillList,promotionGoodsSelectSign);

                }else {
                    et_Money.clearFocus();
                    setSelfNumKeyViewEdit(sf_containt,et_phone,mainActivity);
                }
            }
        });

        AlertDialog.Builder dialog = new AlertDialog.Builder(mainActivity);
        dialog.setView(view).setCancelable(false).create();
        mDialog = dialog.show();

    }

    public static void setSelfNumKeyViewEdit(SelfNumKeyView sf_containt,EditText editText,Activity activity){
        sf_containt.setEnterReceptor(editText);
        sf_containt.setIsCanInputPoint(false);

        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        CommonUtils.hideSoftInputButWithSelection(editText,activity);
    }

    /**
     * 操作提示框
     * 需监听点击事件
     * @param mContext
     * @param dialogTitle 提示标头
     * @param dialogMsg 提示内容
     * @param listener  点击事件监听
     */
    public static void  showInfoDialog(Context mContext, String dialogTitle, String dialogMsg, final OnDialogListener listener) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_tips_info, null);
        TextView tvTitle = view.findViewById(R.id.tv_dialog_title_name);
        tvTitle.setText(dialogTitle);
        tvTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); //textview的drawableRight

        TextView tv_msg = view.findViewById(R.id.tv_info_tips);//提示信息

        Button btnSure = view.findViewById(R.id.btn_tips_ok);//确定
        Button btnCancel = view.findViewById(R.id.btn_tips_back);//返回

        tv_msg.setText(dialogMsg);

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                listener.onClick(DialogConstant.DIALOG_BTN_CANCEL);
            }
        });

        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                listener.onClick(DialogConstant.DIALOG_BTN_SURE);
            }
        });

        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setView(view).setCancelable(false).create();
        mDialog = dialog.show();
    }

    /**
     * 提示信息弹框
     * @param mContext
     * @param titleMsg 标头
     * @param tips 提示信息
     */
    public static void  showMsgInto(Activity mContext,String titleMsg, String tips) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_tips_info_ok, null);
        TextView tvTitle = view.findViewById(R.id.tv_dialog_title_name);
        tvTitle.setText(titleMsg);
        tvTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); //textview的drawableRight

        TextView tv_msg = view.findViewById(R.id.tv_info_tips);//提示信息
        Button btnSure = view.findViewById(R.id.btn_tips_ok);//确定

        tv_msg.setText(tips+"");

        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });

        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setView(view).setCancelable(false).create();
        mDialog = dialog.show();

    }

    /**
     * 满赠 换购信息弹框
     * @param mainActivity
     * @param ticketInfo
     */
    public static void  showPromationoodInfosList(final MainActivity mainActivity, final ExchangeGoodBillInfoListAdapter exchangeAdapter,
                                                  final List<PromationoodInfo> goodsBillList, final List<PromationoodInfo> giftGoodsBillList, final TicketInfo ticketInfo, final boolean selectGiftGoods) {
        View view = LayoutInflater.from(mainActivity).inflate(R.layout.dialog_gift_good, null);
        RecyclerView defaultRecyclerView = view.findViewById(R.id.rv_main_goodList);
        defaultRecyclerView.setLayoutManager(new GridLayoutManager(mainActivity, 1));
        final LinearLayout ll_goodsBillList= view.findViewById(R.id.title_goodsBillList);
        LinearLayout ll_giftGoods = view.findViewById(R.id.ll_title_giftGoods);
        TextView tvName = view.findViewById(R.id.tv_dialog_title_name);
        tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mExchangeGoodBillInfoList.clear();
                mGiftGoodsBillList.clear();
                mainActivity.setSelectGiftGoods(false);
                mDialog.dismiss();
            }
        });

        Button btnSure = view.findViewById(R.id.btn_budget_ok);//确定
        Button btnCancel = view.findViewById(R.id.btn_budget_back);//返回

        defaultRecyclerView.setAdapter(exchangeAdapter);
        if (giftGoodsBillList.size() > 0 && !mainActivity.selectGiftGoods){
            exchangeAdapter.setisExchangeGoods(false);
            tvName.setText("选择买赠");
            exchangeAdapter.setNewData(giftGoodsBillList);
            ll_goodsBillList.setVisibility(View.GONE);
            ll_giftGoods.setVisibility(View.VISIBLE);
        }else {
            mainActivity.setSelectGiftGoods(true);
            ll_goodsBillList.setVisibility(View.VISIBLE);
            ll_giftGoods.setVisibility(View.GONE);
            exchangeAdapter.setisExchangeGoods(true);
            tvName.setText("选择换购");
            exchangeAdapter.setNewData(goodsBillList);
        }

        exchangeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (giftGoodsBillList.size() > 0 && !mainActivity.selectGiftGoods){
                    String saleCount = giftGoodsBillList.get(position).getSaleCount();
                    String maxSaleCount = giftGoodsBillList.get(position).getMaxSaleCount();
                    if (MoneyUtils.getMoneyInt(saleCount) >= MoneyUtils.getMoneyInt(maxSaleCount)){
                        ToastUtils.showMsg("此商品最多能赠送:" + maxSaleCount + "个");
                        return;
                    }

                    giftGoodsBillList.get(position).setSaleCount(CommonUtils.getInt(giftGoodsBillList.get(position).getSaleCount()) + 1 + "");
                    exchangeAdapter.setNewData(giftGoodsBillList);
                }else {
                    String saleCount = goodsBillList.get(position).getSaleCount();
                    String maxSaleCount = goodsBillList.get(position).getMaxSaleCount();
                    if (MoneyUtils.getMoneyInt(saleCount) >= MoneyUtils.getMoneyInt(maxSaleCount)){
                        ToastUtils.showMsg("此商品最多能换购" + maxSaleCount + "个");
                        return;
                    }
                    //把其他换购数量清空
                    for (int i = 0; i < goodsBillList.size(); i++) {
                        if (i != position){
                            goodsBillList.get(i).setSaleCount("0");
                        }
                    }
                    goodsBillList.get(position).setSaleCount(CommonUtils.getInt(goodsBillList.get(position).getSaleCount()) + 1 + "");
                    exchangeAdapter.setNewData(goodsBillList);
                }
            }
        });


        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                mainActivity.setSelectGiftGoods(true);
            }
        });

        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                mExchangeGoodBillInfoList.clear();
                mGiftGoodsBillList.clear();
                if (goodsBillList.size() > 0 && !mainActivity.selectGiftGoods){
                    mainActivity.setSelectGiftGoods(true);
                    showPromationoodInfosList(mainActivity,exchangeAdapter,goodsBillList,
                            giftGoodsBillList,ticketInfo,selectGiftGoods);
                }else {
                    for (int i = 0; i < goodsBillList.size(); i++) {
                        if (MoneyUtils.getMoneyInt(goodsBillList.get(i).getSaleCount()) >0){
                            mExchangeGoodBillInfoList.add(goodsBillList.get(i));
                        }
                    }

                    for (int i = 0; i < giftGoodsBillList.size(); i++) {
                        if (MoneyUtils.getMoneyInt(giftGoodsBillList.get(i).getSaleCount()) >0){
                            mGiftGoodsBillList.add(giftGoodsBillList.get(i));
                        }
                    }
                    promotionGoodsSelectSign = 1;
                    mainActivity.upLoadGoodsPreparePay(false,mExchangeGoodBillInfoList,mGiftGoodsBillList,promotionGoodsSelectSign);

                }
            }
        });

        AlertDialog.Builder dialog = new AlertDialog.Builder(mainActivity);
        dialog.setView(view).setCancelable(false).create();
        mDialog = dialog.show();

        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
//        params.width = 200;
        params.height = 600 ;
        mDialog.getWindow().setAttributes(params);

    }


}
